package co.za.itweb.defenceweb;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.GradientDrawable.Orientation;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class Latest extends FragmentActivity {

	static String[] titles;
	static String[] images;
	static String[] bigimages;
	static String[] story;
	static String[] published;
	static String[] summary;
	static String[] author;
	String title;
	ListView list;
	Lazy2 adapterLatest;
	int feedValue;

	Dialog dialog;
	ProgressDialog progressDialog;
	RSSFeed feed;

	

	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.latest_layout);
		feedValue = 0;
		System.out
				.println("////////////////////////////////////////////////////////////////////////LATESSSSTTTTT");
		if (feedValue == 0) {
			title = "Latest News";
		}


		LinearLayout layout = (LinearLayout) findViewById(R.id.cell5);
		 int colors[] =  {Color.parseColor("#FF4a79bc"),Color.parseColor("#FF33558d")};
		GradientDrawable grad = new GradientDrawable(Orientation.TOP_BOTTOM,
				colors);
		layout.setBackgroundDrawable(grad);
		TextView titletext = (TextView) findViewById(R.id.title);
		titletext.setText(title);

		@SuppressWarnings("unused")
		ImageButton back = (ImageButton) findViewById(R.id.cell5).findViewById(
				R.id.back);
		ImageButton refresh = (ImageButton) findViewById(R.id.cell5)
				.findViewById(R.id.refresh);
		ImageButton info = (ImageButton) findViewById(R.id.cell5).findViewById(
				R.id.info);
		TextView menutext = (TextView) findViewById(R.id.cell5).findViewById(
				R.id.menutext);
		menutext.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent1 = new Intent(Latest.this, FirstScreen.class);
				intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(intent1);
				finish();

			}
		});
		

		refresh.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(Latest.this, RefreshLatest.class);
				startActivityForResult(intent, 1);


			}
		});

		info.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {

				Intent intent = new Intent(Latest.this, DialogActivity.class);
				startActivity(intent);

				

			}
		});
		if (FirstScreen.fList.size() < feedValue) {
			Toast.makeText(getApplicationContext(), "Feed is not loaded",
					Toast.LENGTH_LONG).show();

			finish();
		} else {

			titles = new String[FirstScreen.fList.get(feedValue)._itemlist
					.size()];
			images = new String[FirstScreen.fList.get(feedValue)._itemlist
					.size()];
			bigimages = new String[FirstScreen.fList.get(feedValue)._itemlist
					.size()];
			story = new String[FirstScreen.fList.get(feedValue)._itemlist
					.size()];
			published = new String[FirstScreen.fList.get(feedValue)._itemlist
					.size()];
			summary = new String[FirstScreen.fList.get(feedValue)._itemlist
					.size()];
			author = new String[FirstScreen.fList.get(feedValue)._itemlist
					.size()];

			for (int i = 0; i < FirstScreen.fList.get(feedValue)._itemlist
					.size(); i++) {
				if (FirstScreen.fList.get(feedValue)._itemlist.get(i)
						.getTitle() != null) {
					titles[i] = FirstScreen.fList.get(feedValue)._itemlist.get(
							i).getTitle();
				} else {
					titles[i] = "";
				}
				if (FirstScreen.fList.get(feedValue)._itemlist.get(i)
						.getImage() != null) {
					images[i] = FirstScreen.fList.get(feedValue)._itemlist.get(
							i).getImage();
				} else {
					images[i] = "";
				}

				if (FirstScreen.fList.get(feedValue)._itemlist.get(i)
						.getbigImage() != null) {
					bigimages[i] = FirstScreen.fList.get(feedValue)._itemlist
							.get(i).getbigImage();
				} else {
					bigimages[i] = "";
				}
				if (FirstScreen.fList.get(feedValue)._itemlist.get(i)
						.getStory() != null) {
					story[i] = FirstScreen.fList.get(feedValue)._itemlist
							.get(i).getStory().toString();
				} else {
					story[i] = "";
				}
				if (FirstScreen.fList.get(feedValue)._itemlist.get(i)
						.getpubDate() != null) {
					published[i] = FirstScreen.fList.get(feedValue)._itemlist
							.get(i).getpubDate();
				} else {
					published[i] = "";
				}
				if (FirstScreen.fList.get(feedValue)._itemlist.get(i)
						.getSummary() != null) {
					summary[i] = FirstScreen.fList.get(feedValue)._itemlist
							.get(i).getSummary();
				} else {
					summary[i] = "";
				}
				if (FirstScreen.fList.get(feedValue)._itemlist.get(i)
						.getAuthor() != null) {
					author[i] = FirstScreen.fList.get(feedValue)._itemlist.get(
							i).getAuthor();
				} else {
					author[i] = "";
				}
			}

			list = (ListView) findViewById(R.id.list);
			adapterLatest = new Lazy2(this, images);
			list.setAdapter(adapterLatest);
			list.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					Intent intent = new Intent(Latest.this, SingleLatest.class);
					intent.putExtra("id", position);
					intent.putExtra("feedid", 0);
					startActivity(intent);
				}
			});

		}
	}

	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			Intent intent1 = new Intent(Latest.this, FirstScreen.class);
			intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent1);
			finish();
			return true;

		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void onBackPressed() {

		Intent intent1 = new Intent(Latest.this, FirstScreen.class);
		intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivity(intent1);
		finish();

		return;
	}

}
